<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2024/9/20
  Time: 上午12:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>修改用户信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <!-- 用户ID -->
    <div class="layui-form-item">
        <label class="layui-form-label">用户ID</label>
        <div class="layui-input-block">
            <input type="text" id="id" name="id" lay-verify="required" placeholder="" class="layui-input" readonly>
        </div>
    </div>

    <!-- 用户姓名 -->
    <div class="layui-form-item">
        <label class="layui-form-label">用户姓名</label>
        <div class="layui-input-block">
            <input type="text" id="name" name="name" lay-verify="required" placeholder="" class="layui-input">
        </div>
    </div>

    <!-- 性别 -->
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <select id="gender" name="gender">
                <option value="">请选择</option>
                <option value="男">男</option>
                <option value="女">女</option>
            </select>
        </div>
    </div>

    <!-- 所属学院 -->
    <div class="layui-form-item">
        <label class="layui-form-label">所属学院</label>
        <div class="layui-input-block">
            <select id="college_id" name="college_id">
                <option value="">--请选择--</option>
                <option value="1">机电工程学院</option>
                <option value="2">机械工程学院</option>
                <option value="3">材料科学与工程学院</option>
                <option value="4">化学工程与技术学院</option>
                <option value="5">信息与通信工程学院</option>
                <option value="6">仪器与电子学院</option>
                <option value="7">大数据学院</option>
                <option value="8">理学院</option>
                <option value="9">经济与管理学院</option>
                <option value="10">人文社会科学学院</option>
                <option value="11">体育学院</option>
                <option value="12">艺术学院</option>
                <option value="13">软件学院</option>
                <option value="14">环境与安全工程学院</option>
                <option value="15">电气与控制工程学院</option>
                <option value="16">能源动力工程学院</option>
            </select>
        </div>
    </div>

    <!-- 身份 -->
    <div class="layui-form-item">
        <label class="layui-form-label">身份</label>
        <div class="layui-input-block">
            <select id="identity" name="identity">
                <option value="">请选择</option>
                <option value="1">管理员</option>
                <option value="2">教师</option>
                <option value="3">学生</option>
            </select>
        </div>
    </div>

    <!-- 手机号码 -->
    <div class="layui-form-item">
        <label class="layui-form-label">手机号码</label>
        <div class="layui-input-block">
            <input type="text" id="phone_number" name="phone_number" placeholder="" class="layui-input">
        </div>
    </div>

    <!-- 密码 -->
    <div class="layui-form-item">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="text" id="password" name="password" placeholder="" value="" class="layui-input">
        </div>
    </div>

    <!-- 提交按钮 -->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="editUserBtn">保存修改</button>
        </div>
    </div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'jquery'], function () {
        var form = layui.form,
            $ = layui.jquery;

        // 获取用户ID
        var userId = new URLSearchParams(window.location.search).get('id');

        if (userId) {
            $.ajax({
                url: '${pageContext.request.contextPath}/userServlet',
                type: 'GET',
                data: { method: 'getUserById', id: userId },
                success: function (response) {
                    var data = JSON.parse(response);
                    if (data.code === 0) {
                        var user = data.data;
                        // 填充表单
                        $('#id').val(user.id);
                        $('#name').val(user.name);
                        $('#gender').val(user.gender);
                        $('#college_id').val(user.college_id);
                        $('#identity').val(user.identity);
                        $('#phone_number').val(user.phone_number);

                        form.render(); // 重新渲染表单
                    } else {
                        alert("用户信息获取失败");
                    }
                },
                error: function () {
                    alert("请求失败");
                }
            });
        } else {
            alert("未获取到用户ID");
        }

        // 监听提交事件
        form.on('submit(editUserBtn)', function (data) {
            var formData = data.field;
            $.ajax({
                url: '${pageContext.request.contextPath}/userServlet',
                type: 'POST',
                data: {
                    method: 'modifyUser',
                    data: JSON.stringify(formData)
                },
                success: function (response) {
                    if (response == 1) {
                        layer.msg('修改成功', { icon: 1, time: 1000 }, function () {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.location.reload();
                        });
                    } else {
                        layer.msg('修改失败', { icon: 2 });
                    }
                },
                error: function () {
                    layer.msg('请求失败', { icon: 2 });
                }
            });
            return false; // 阻止表单默认提交
        });
    });
</script>
</body>
</html>
